#include "labelparser.h"
#include <QTextStream>
#include <QByteArray>
#include <QXmlStreamWriter>
#include <QXmlStreamAttributes>
#include <QStringList>
#include <QDebug>

int LabelParser::_id = 1;
int LabelParser::_word = 1;
LabelParser* LabelParser::_instance = 0;

LabelParser* LabelParser::Instance()
{
    if(!_instance){
        _instance = new LabelParser(0);
    }

    return _instance;
}


LabelParser::LabelParser(QObject *parent) :
    QObject(parent)
{
}

QByteArray LabelParser::parse(QTextStream &in)
{
    QByteArray out;
    QXmlStreamWriter xmlWriter(&out);

    xmlWriter.setAutoFormatting(true);
    xmlWriter.writeStartDocument();
    xmlWriter.writeStartElement("smil");
    xmlWriter.writeAttribute("xmlns","http://www.w3.org/ns/SMIL");
    xmlWriter.writeAttribute("xmlns:epub","http://www.idpf.org/2007/ops");
    xmlWriter.writeAttribute("version","3.0");

    xmlWriter.writeStartElement("seq");
    xmlWriter.writeAttribute("id",nextID());
    xmlWriter.writeAttribute("epub:textref",seqRef);
    xmlWriter.writeAttribute("epub:type","bodymatter chapter");

    QString text = in.readAll();
    QStringList lines = text.split(0x0D);
    QString line;

    //qDebug()<<lines;


    foreach (line, lines){
        qDebug()<<line;

        if(line.isEmpty()){
            break;
        }
        QStringList data = line.split('\t');

        if(data.size() != 3){
            return QByteArray();
        }

        xmlWriter.writeStartElement("par");
        xmlWriter.writeAttribute("id",nextID());

        xmlWriter.writeStartElement("text");
        QString src=seqRef+"#word"+QString::number(_word++);
        xmlWriter.writeAttribute("src",src);
        xmlWriter.writeEndElement();//text
        xmlWriter.setAutoFormatting(false);
        xmlWriter.writeComment(data[2]);
        xmlWriter.setAutoFormatting(true);

        xmlWriter.writeStartElement("audio");
        xmlWriter.writeAttribute("src",audioSrc);
        xmlWriter.writeAttribute("clipBegin",seconds2Stamp(data[0]));
        xmlWriter.writeAttribute("clipEnd",seconds2Stamp(data[1]));
        xmlWriter.writeEndElement(); //audio

        xmlWriter.writeEndElement();//par

    }



    xmlWriter.writeEndElement();//seq
    xmlWriter.writeEndElement(); //smil
    xmlWriter.writeEndDocument();

    return out;
}

QString LabelParser::nextID()
{
    QString id="id";
    return id+QString::number(++_id);
}

QString LabelParser::seconds2Stamp(const QString &seconds)
{
    float t_sec = seconds.toFloat();
    unsigned hours = t_sec / (60 * 60);
    unsigned mins = t_sec / 60;
    float secs= t_sec - hours*60*60 - mins*60;

    QString sHour = QString::number(hours);
    if(hours < 10) {
        sHour = '0' + sHour;
    }
    QString sMin = QString::number(mins);
    if(mins < 10){
        sMin = '0' + sMin;
    }
    QString sSec = QString::number(secs,'g',4);
    if(secs < 10) {
        sSec = '0' + sSec;
    }

    QString ret = sHour + ':' + sMin + ':' + sSec;

    return ret;
}

LabelParser::~LabelParser()
{
    _instance = 0;
}
